package com.atsansan.mywork.login.controller;

import com.atsansan.mywork.login.entity.UserEntity;
import com.atsansan.mywork.login.service.BookService;
import com.atsansan.mywork.login.service.CourseraService;
import com.atsansan.mywork.login.service.UserService;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.api.R;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author sansan
 * @create 2021-01-12 7:39
 */
@Controller
@RequestMapping("user/user")
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private BookService bookService;

    @Autowired
    private CourseraService courseraService;
//    @Autowired
//    private BaseMapper baseMapper;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(@RequestParam("name") String name,
                        @RequestParam("password") String password, Map<String,Object> map){

//        baseMapper.selectByName(name);
//        List<UserEntity> list = userService.list();
        UserEntity userEntity = userService.login(name, password);
        List<String> bookLevel = bookService.getBookLevel();
        map.put("bookLevel",bookLevel);
        if (!(userEntity==null)){
            //成功
            if (userEntity.getStatus()==1){
                return "redirect:/index2";
            }else {
                return "login_success";
            }
//            return "login_success";

        }else {
            //失败
            map.put("msg","失败");

            return "redirect:/error1";
//            return "redirect:/login";
        }
    }
    public String login(HttpServletRequest request){

        String name = request.getParameter("name");
        String password = request.getParameter("password");
        System.out.println(name+password);
        return "ok";
    }
    public User info(){
        UserEntity entity = userService.getById(1);
        System.out.println(entity);
        return null;
    }

}
